package com.zking.myservlet.news;

import com.zking.dao.NewsDao;
import com.zking.entity.News;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/manage/newsAdd")
public class NewsAdd extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理请求的编码，解决中文乱码
        request.setCharacterEncoding("utf-8");
        //处理响应的编码 ，解决中文乱码
        response.setContentType("text/html;charset=utf-8");

        System.out.println("NewsAdd===进来");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        News news = new News(null, title, content, null);
        boolean flag = NewsDao.addNews(news);
        if(flag){
            int pageSize = 2;
            int maxPage = NewsDao.getMaxPage(pageSize);
            response.sendRedirect("newsSelectByPage?pageIndex="+maxPage);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
